import { getSearchActionApi, getSuggestionApi } from '@/api/search';
import { AppDispatch } from '..';
import { SearchActionType } from '../reducers/search';

export const getSuggestAction = (q: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await getSuggestionApi(q);
    dispatch({ type: 'search/saveList', payload: res.data.options });
    // console.log(8, res);
  };
};
// 清空历史
export const clearSuggestAction = (): SearchActionType => {
  return {
    type: 'search/clearList',
  };
};
// 历史记录
export const saveHistoryAction = (payload: string): SearchActionType => {
  return {
    type: 'search/saveHistory',
    payload,
  };
};
// 5. 定义action
export const clearHistoriesAction = (): SearchActionType => {
  return {
    type: 'search/clearHistories',
  };
};
export const getResultsAction = (keyword: string, page = 1, per_page = 10) => {
  return async (dispatch: AppDispatch) => {
    const res = await getSearchActionApi({ q: keyword, page, per_page });
    dispatch({ type: 'search/saveResults', payload: res.data.results });
  };
};
export const clearResultsAction = () => {
  return {
    type: 'search/clearResults',
  };
};
